package com.wells.slide.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtil
{
    public enum NET_STATE
    {
        NETWORK_WIFI,
        NETWORK_3G,
        NETWORK_WIMAX,
        NETWORK_NONE
    }

    public static NET_STATE checkStatus( Context context )
    {
        final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService( Context.CONNECTIVITY_SERVICE );
        
        try
        {
            // 3G
            NetworkInfo mobile = connMgr.getNetworkInfo( ConnectivityManager.TYPE_MOBILE );

            // WIFI
            NetworkInfo wifi = connMgr.getNetworkInfo( ConnectivityManager.TYPE_WIFI );

            // LTE
            NetworkInfo wimax = connMgr.getNetworkInfo( ConnectivityManager.TYPE_WIMAX );

            boolean isWimax = false;

            if ( wimax != null )
            {
                isWimax = wimax.isConnected();
            }

            if ( wifi.isConnected() )
            {
                return NET_STATE.NETWORK_WIFI;
            }
            else if ( mobile.isConnected() )
            {
                return NET_STATE.NETWORK_3G;
            }
            else if ( isWimax )
            {
                return NET_STATE.NETWORK_WIMAX;
            }
            else
            {
                return NET_STATE.NETWORK_NONE;
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            return NET_STATE.NETWORK_NONE;
        }
    }

    public static boolean isConnected( Context context )
    {
        final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService( Context.CONNECTIVITY_SERVICE );
        boolean result = false;

        try
        {
            // 3G
            NetworkInfo mobile = connMgr.getNetworkInfo( ConnectivityManager.TYPE_MOBILE );

            // WIFI
            NetworkInfo wifi = connMgr.getNetworkInfo( ConnectivityManager.TYPE_WIFI );

            // LTE
            NetworkInfo wimax = connMgr.getNetworkInfo( ConnectivityManager.TYPE_WIMAX );

            boolean isWimax = false;

            if ( wimax != null )
            {
                isWimax = wimax.isConnected();
            }

            if ( wifi.isConnected() || mobile.isConnected() || isWimax )
            {
                result = true;
            }
            else
            {
                result = false;
            }
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            result = false;
        }

        return result;

    }
}
